package com.lening.common.config;

import com.lening.common.util.LocalDateUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * @author 李猛
 * @datetime 2024/4/18 8:42
 * @description 类对象
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    /*@Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new PermissionInterceptor())//添加拦截器
                .addPathPatterns("/**")//要拦截的路径
                .excludePathPatterns("/user/register", "/user/login","/user/logout");//哪些路径不拦截
    }*/

//    @Override
//    public void addCorsMappings(CorsRegistry registry) {
//        registry.addMapping("/**")//要映射的路径
//                .allowedMethods("GET", "POST", "PUT", "DELETE");//允许跨域的请求方式
//    }

    /**
     * 数据转换
     *
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
        SimpleModule simpleModule = new SimpleModule();

        //1.转换
//        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
//        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);

        //2.日期-响应
        simpleModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(LocalDateUtil.DATE_FORMATTER_4));
        simpleModule.addSerializer(LocalDate.class, new LocalDateSerializer(LocalDateUtil.DATE_FORMATTER_5));
        simpleModule.addSerializer(LocalTime.class, new LocalTimeSerializer(LocalDateUtil.DATE_FORMATTER_6));

        //3.日期-请求
        simpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(LocalDateUtil.DATE_FORMATTER_4));
        simpleModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(LocalDateUtil.DATE_FORMATTER_5));
        simpleModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(LocalDateUtil.DATE_FORMATTER_6));

        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(0, jackson2HttpMessageConverter);
    }

    /**
     * 配置静态资源
     *
     * @param registry
     */
    /*@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**").addResourceLocations("file:d:\\data\\");
    }*/
}
